package com.itheima.threadmethod2;

public class MyThread extends Thread {
    static int ticket = 0;
    //使用锁
    static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                //同步代码块
                if (ticket <= 100) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "正在卖出第" + ticket + "张票");
                } else {
                    break;
                }
            }
        }
    }
}
